﻿using HW.Core.Helper;
using HW.Core.Ioc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW.Core.Config
{
    [ExposedService(Lifetime.Singleton, typeof(IConfigManager))]
    public class ConfigManager : IConfigManager
    {
        private const string root = "Config";
        private string GetFullPath(ValueType key)
        {
            return Path.Combine(root, key.GetType().FullName + "." + key.ToString() + ".json");
        }

        public T Read<T>(ValueType key)
        {
            string filename = GetFullPath(key);
            return JsonHelper.Load<T>(filename);
        }

        public void Write<T>(ValueType key, T value)
        {
            Assert.NotNull(key);
            Assert.NotNull(value);
            Directory.CreateDirectory(root);
            string filename = GetFullPath(key);
            JsonHelper.Save(value, filename, true);
        }
    }
}
